// pages/yewu/kqtj/index.js
var utils = require("../../../utils/util.js")
var my = require("../../../utils/MyUtils.js")
Page({
  /**
   * 留存问题：根据后台获取的信息来判断是否删除这一天
   */
  /**
   * 页面的初始数据
   */
  data: {
    // hasDay: false,
    yearIndex: 0,
    monIndex: (new Date().getMonth()) - 1, //,
    showPopup: true,
    b: 0,
    calendarMark: {
      zc: [],
      qk: [], //缺卡
      wc: [], //外出
      jiaban: [], //加班
      chidao: [], //迟到
      qingjia: [], //请假
      chuchai: [], //出差
      kuanggong: [], //旷工
      wu: []
    },
    ycbc: [{
        id: 1,
        value: '补卡申请'
      },
      {
        id: 2,
        value: '出差申请'
      },
      {
        id: 3,
        value: '加班申请'
      },
      {
        id: 4,
        value: '请假申请'
      },
      {
        id: 5,
        value: '外出申请'
      },
    ],
    // dk:null,
    dk: {},
    status3: false,
    status1: true,
    status2: false,
    date: new Date().getFullYear() + "" + utils.formatNumber(new Date().getMonth() + 1),
    dateNow: new Date().getFullYear() + "-" + utils.formatNumber(new Date().getMonth() + 1),
    dateNow2: new Date().getFullYear() + "" + utils.formatNumber(new Date().getMonth() + 1),
    next: false,
    tabScrollTop: 0,
  },
  getTabScrollTop: function (e) {
    this.setData({
      tabScrollTop: e.detail.y,
    })
  },
  //监听用户滑动页面事件
  onPageScroll: function (e) {
    var buttonStutes = false
    var status4 = false
    if (e.scrollTop > this.data.tabScrollTop) {
      if (e.scrollTop >= 1) {
        status4 = true
        buttonStutes = true;
      } else {
        status4 = false
        buttonStutes = true
      }
      // 滚动到最底部
      e.scrollTop = 0;
    } else {
      var query = wx.createSelectorQuery()
      query.select("#alll").boundingClientRect()
      query.selectViewport().scrollOffset()
      query.exec((res) => {
        if (res[0].height < wx.getSystemInfoSync().windowHeight - 75) {
          if (res[0].height + 300 < wx.getSystemInfoSync().windowHeight) {
            return false
          } else {
            if (e.scrollTop > this.data.tabScrollTop) {
              e.scrollTop = 0; // 滚动到最底部
              buttonStutes = true;
            }
          }
        } else {
          if (e.scrollTop > this.data.tabScrollTop) {
            // 滚动到最底部
            buttonStutes = true;
            if (res[0].height + 75 > wx.getSystemInfoSync().windowHeight) {
              status4 = false
            } else {
              buttonStutes = false
            }
          } else {
            if (res[0].height < wx.getSystemInfoSync().windowHeight - 75) {
              this.setData({
                status4: false,
                scrollTop: e.scrollTop,
                buttonStuts: true
              })
              // return true
            } else {
              e.scrollTop = 0;
              buttonStutes = true;
              status4 = false;
            }
          }
        }
      })
    }
    this.setData({
      scrollTop: e.scrollTop,
      status4,
      buttonStuts: buttonStutes
    })
  },
  yes(e) {
    var url = ""
    this.setData({
      showPopup: !this.data.showPopup
    })
    if (e.detail == "1") {
      url = "../../work/repCard/index"
    } else if (e.detail == "2") {
      url = "../../work/businessTrip/index"
    } else if (e.detail == "3") {
      url = "../../work/add/add"
    } else if (e.detail == "4") {
      url = "../../work/leave/index"
    } else if (e.detail == "5") {
      url = "../../work/leaveOut/index"
    }
    wx.navigateTo({
      url: url,
    })
  },
  no() {
    this.setData({
      showPopup: !this.data.showPopup
    })
  },
  /**
   * 获取点击天数的打卡信息
   * @param {*} e 
   */
  onCalendarGetDate(e) {
    //创建节点选择器
    var query = wx.createSelectorQuery();
    query.select('.r2').boundingClientRect()
    query.selectViewport().scrollOffset()
    query.exec((res) => {
      var listHeight = res[0].height*2+90+"px"; // 获取list高度
      this.setData({
        listHeight
      })
    });
    var _TOKEN = wx.getStorageSync('_TOKEN')
    var t = this
    var dk = this.data.dk
    my.request("/oa-applet/attendance-calendar", "get", {
      date: e.detail.selected
    }, {
      token: _TOKEN
    }, function (res) {
      wx.hideLoading()
      if (res.data.code == 0) {
        dk = res.data.data[0]
        t.setData({
          status3: true,
          dk: dk
        })
      } else {
        my.alert(res.data.message)
      }
    })
  },
  ceshi(res) {
    wx.hideLoading()
    var t = this
    var calendarMark = this.data.calendarMark
    if (res.data.code == 0) {
      var data = res.data.data
      for (var i = 0; i < data.length; i++) {
        switch (data[i].attendance_type) {
          case 0:
            calendarMark.wu.push(t.data.dateNow + "-" + data[i].work_date);
            break;
          case 1:
            calendarMark.zc.push(t.data.dateNow + "-" + data[i].work_date);
            break;
          case 2:
            calendarMark.qk.push(t.data.dateNow + "-" + data[i].work_date);
            break;
          case 3:
            calendarMark.wc.push(t.data.dateNow + "-" + data[i].work_date);
            break;
          case 4:
            calendarMark.jiaban.push(t.data.dateNow + "-" + data[i].work_date);
            break;
          case 5:
            calendarMark.chidao.push(t.data.dateNow + "-" + data[i].work_date);
            break;
          case 6:
            calendarMark.qingjia.push(t.data.dateNow + "-" + data[i].work_date);
            break;
          case 7:
            calendarMark.chuchai.push(t.data.dateNow + "-" + data[i].work_date);
            break;
          case 8:
            calendarMark.kuanggong.push(t.data.dateNow + "-" + data[i].work_date);
            break;
        }
      }
      t.setData({
        calendarMark: calendarMark
      })
    } else {
      my.alert(res.data.message)
    }
  },
  /**
   * 根据当前月份判断是否显示next按钮，
   * 并向后台请求数据并把数据加到dayStyle中
   * 需要用到 dayStyle.push()
   * @param {*} e 
   */
  prev: function (e) {
    this.setData({
      dateNow: e.detail.currentYear + "-" + utils.formatNumber(e.detail.currentMonth),
      dateNow2: e.detail.currentYear + "" + utils.formatNumber(e.detail.currentMonth),
      dayStyle: []
    })
    var _TOKEN = wx.getStorageSync('_TOKEN')
    if (my.bijiao(this.data.dateNow2, this.data.date)) {
      this.setData({
        status3: false
      })
    } else {
      this.setData({
        status3: false
      })
    }
    my.request("/oa-applet/attendance-month-calendar", "get", {
      date: this.data.dateNow
    }, {
      token: _TOKEN
    }, this.ceshi)
  },
  /**
   * 查看月度统计
   */
  bk: function () {
    var that = this
    if (!that.data.status1) {
      that.setData({
        status1: !this.data.status1,
        status2: false
      })
      this.onLoad()
    }
  },
  /**
   * 查看考勤月历
   */
  ck: function () {
    var _TOKEN = wx.getStorageSync('_TOKEN')
    var that = this
    if (!that.data.status2) {
      my.request("/oa-applet/attendance-month-calendar", "get", {
        date: this.data.dateNow
      }, {
        token: _TOKEN
      }, that.ceshi)
      this.setData({
        status2: !this.data.status2,
        status1: false
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var _TOKEN = wx.getStorageSync('_TOKEN')
    if (_TOKEN == '') {
      wx.redirectTo({
        url: '../../login/login',
      })
    }
    // console.log(my.monArr().splice(0, new Date().getMonth()))
    this.setData({
      year: my.yearArr(),
      mon: my.monArr().splice(0, new Date().getMonth()),
      yearNow: new Date().getFullYear() + "年",
      monNow: new Date().getMonth() + 1 > 10 ? new Date().getMonth() + 1 + "月" : "0" + (new Date().getMonth() + 1) + "月",
      yearNow2: new Date().getFullYear(),
    })
    // console.log(this.data.mon[this.data.monIndex])
    this.setData({
      tt: utils.getMDC(new Date()),
      monNow2: this.data.mon[this.data.monIndex].substring(0, 2)
    })
    this.MyFun()
  },
  MyFun() {
    var _TOKEN = wx.getStorageSync('_TOKEN')
    var t = this
    // debugger
    my.request("/oa-applet/attendance-count", "get", {
      date: this.data.yearNow2 + "-" + this.data.monNow2
    }, {
      token: _TOKEN
    }, this.suc)
    my.request("/oa-applet/attendance-count-detail", "get", {
      date: t.data.yearNow2 + "-" + t.data.monNow2
    }, {
      token: _TOKEN
    }, function (res) {
      wx.hideLoading()
      if (res.data.code != -1) {
        var data = {
          late_detail: {
            data: res.data.data.late_detail,
            show: null,
          },
          leave_early_detail: {
            data: res.data.data.leave_early_detail,
            show: null,
          },
          norecord_detail: {
            data: res.data.data.norecord_detail,
            show: null,
          },
          absent_detail: {
            data: res.data.data.absent_detail,
            show: null,
          },
        }
        for (var i = 0; i < data.late_detail.data.length; i++) {
          var date = data.late_detail.data[i].late_date
          data.late_detail.data[i].week = utils.getWeek(new Date(date))
          data.late_detail.data[i].late_time = data.late_detail.data[i].late_time % 60 == 0 ? parseInt(data.late_detail.data[i].late_time / 60) :
            parseInt(data.late_detail.data[i].late_time / 60) + 1
          data.late_detail.show = false
        }
        for (var i = 0; i < data.leave_early_detail.data.length; i++) {
          var date = data.leave_early_detail.data[i].late_date
          data.leave_early_detail.data[i].week = utils.getWeek(new Date(date))
          data.leave_early_detail.data[i].late_time = (data.leave_early_detail.data[i].late_time) % 60 == 0 ? -parseInt(data.leave_early_detail.data[i].late_time / 60) : -parseInt(data.leave_early_detail.data[i].late_time / 60) + 1
          data.leave_early_detail.show = false
        }
        for (var i = 0; i < data.norecord_detail.data.length; i++) {
          var date = data.norecord_detail.data[i].work_date
          data.norecord_detail.data[i].week = utils.getWeek(new Date(date))
          data.norecord_detail.show = false
        }
        for (var i = 0; i < data.absent_detail.data.length; i++) {
          var date = data.absent_detail.data[i].work_date
          data.absent_detail.data[i].week = utils.getWeek(new Date(date))
          data.absent_detail.show = false
        }
        t.setData({
          attendanceData: data
        })
      } else {
        t.setData({
          attendanceData: null
        })
      }
    })
    my.request("/oa-applet/out-count-detail", "get", {
      date: t.data.yearNow2 + "-" + t.data.monNow2
    }, {
      token: _TOKEN
    }, function (res) {
      wx.hideLoading()
      if (res.data.code != -1) {
        var data = {
          vacation_days: res.data.data.vacation_days,
          out_days: res.data.data.out_days,
          business_days: res.data.data.business_days,
          overtime_days: res.data.data.overtime_days,
          vacation_detail: {
            data: res.data.data.vacation_detail,
            show: null
          },
          out_detail: {
            data: res.data.data.out_detail,
            show: null
          },
          business_detail: {
            data: res.data.data.business_detail,
            show: null
          },
          overtime_detail: {
            data: res.data.data.overtime_detail,
            show: null
          }
        }
        for (var i = 0; i < data.vacation_detail.data.length; i++) {
          var date = data.vacation_detail.data[i].start_date
          data.vacation_detail.data[i].week = utils.getWeek(new Date(date))
          data.vacation_detail.show = true
        }
        for (var i = 0; i < data.out_detail.data.length; i++) {
          var date = data.out_detail.data[i].start_date
          data.out_detail.data[i].week = utils.getWeek(new Date(date))
          data.out_detail.show = true
        }
        for (var i = 0; i < data.business_detail.data.length; i++) {
          var date = data.business_detail.data[i].start_date
          data.business_detail.data[i].week = utils.getWeek(new Date(date))
          data.business_detail.show = true
        }
        for (var i = 0; i < data.overtime_detail.data.length; i++) {
          var date = data.overtime_detail.data[i].start_date
          data.overtime_detail.data[i].week = utils.getWeek(new Date(date))
          data.overtime_detail.show = true
        }
        t.setData({
          outData: data
        })
      } else {
        t.setData({
          outData: null
        })
      }
    })
  },
  suc(res) {
    var t = this
    wx.hideLoading()
    if (res.data.code != -1) {
      t.setData({
        data: res.data.data
      })
    } else {
      t.setData({
        data: null
      })
      my.alert(res.data.message)
    }
  },
  selectYear(e) {
    this.setData({
      yearNow: this.data.year[e.detail.value],
      yearIndex: e.detail.value,
      yearNow2: this.data.year[e.detail.value].substring(0, 4),
      status4: false
    })
  },
  selectMon(e) {
    this.setData({
      monNow: this.data.mon[e.detail.value],
      monIndex: e.detail.value,
      monNow2: this.data.mon[e.detail.value].substring(0, 2),
      status4: false
    })
    this.MyFun()
  },
  showPopup() {
    this.setData({
      showPopup: !this.data.showPopup
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},
  showOrHide(e) {
    var attendanceData = this.data.attendanceData
    var outData = this.data.outData
    switch (e.currentTarget.dataset.id) {
      case 'late_detail':
        attendanceData.late_detail.show = !attendanceData.late_detail.show
        break;
      case 'leave_early_detail':
        attendanceData.leave_early_detail.show = !attendanceData.leave_early_detail.show
        break;
      case 'norecord_detail':
        attendanceData.norecord_detail.show = !attendanceData.norecord_detail.show
        break;
      case 'absent_detail':
        attendanceData.absent_detail.show = !attendanceData.absent_detail.show
        break;
      case 'vacation_detail':
        outData.vacation_detail.show = !outData.vacation_detail.show
        break;
      case 'overtime_detail':
        outData.overtime_detail.show = !outData.overtime_detail.show
        break;
      case 'business_detail':
        outData.business_detail.show = !outData.business_detail.show
        break;
      case 'out_detail':
        outData.out_detail.show = !outData.out_detail.show
        break;
    }
    this.setData({
      attendanceData: attendanceData,
      outData: outData
    })
  }
})